﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Entities;

namespace LevenBlog.Core.Install.Operations {
    public class SqliteUserInstall : IInstallOperation {
        #region IInstallOperation Members

        public string Name {
            get { return CoreResource.install_sqliteuser_name; }
        }

        public string Intro {
            get { return CoreResource.install_sqliteuser_intro; }
        }

        public object Data {
            get;
            set;
        }

        public OperationResult Execute(OperationContext context) {
            List<string> errors = new List<string>();
            try {
                if (!(Data is ICollection<lb_user>)) {
                    errors.Add(CoreResource.install_sqliteuser_dataerror);
                }
                ICollection<lb_user> users = Data as ICollection<lb_user>;
                foreach (var item in users) {
                    context.DbFactory.User.Insert(context.DbProvider, item);
                    item.userinfo.userid = 1;
                    context.DbFactory.UserInfo.Insert(context.DbProvider, item.userinfo);
                    var state = context.DbFactory.State.Get(context.DbProvider);
                    state.usernum++;
                    context.DbFactory.State.Update(context.DbProvider, state);
                }
            } catch (Exception e) {
                errors.Add(e.Message);
            }
            return new OperationResult(errors.Count > 0 ? false : true, errors);
        }

        #endregion
    }
}
